/**
 * Created by quxiangqian on 2018/6/5.
 */
export  class Router extends React.Component{

    state={
        comp:null
    }
    constructor(){
        super();
    }

    load = (path) => {
        let json={...this.props};
        require([path],(n)=>{
            if(n){
                this.state.comp=React.createElement(n.default,{...this.props});
                this.setState({comp:this.state.comp})
            }

        },(e)=>{
                this.state.comp=(<div>未找到组件</div>);
                console.log("未找到组件")
                this.setState({comp:this.state.comp})
        }
        )
    }
    componentDidMount(){

        // let bl=false;
        // this.props.dataSource.map((m)=>{
        //     let path=location.hash;
        //     let temppath=path.replace("#","").split("/");
        //     let hashpath=path.replace("#","");
        //     if(temppath.length>2){
        //         hashpath="/"+temppath[1]
        //     }
        //
        //     if(hashpath===m.path){
        //         this.load(m.component);
        //         this.props.initLoad(m.path);
        //         bl=true;
        //         return ;
        //     }
        // })
        // if(!bl){
        //     this.state.comp=(<div>未找到组件</div>);
        //     console.log("未找到组件")
        //     this.setState({comp:this.state.comp})
        // }

        let path=location.hash;
        if(!path||path===""){
            path="#page/Index"
            location.href=path;
        }
        // let temppath=path.replace("#","").split("/");
        let hashpath=path.replace("#","");
        // if(temppath.length>2){
        //     hashpath="/"+temppath[1]
        // }

        this.load(hashpath)
        this.props.initLoad(hashpath);

     // this.load()
       //this.setState({dataSource:this.props.dataSource});
    }

    render(){
        return (
        <div className="abs-layout">
            {this.state.comp}
        </div>
       )
    }
}